# Esta clase permite menejar los OGX Trainees. Estos se diferencian de los trainees, ya que son trainees locales, que son candidatos a realizar una experiencia en el exterior. 
class OgxtraineesController < ApplicationController

  before_filter :ensure_login
  before_filter :get_mod, :only => [:new,:edit, :delete]

# Recupera todos los ogxtrainees.
  def index
  
   if params[:estado_id] then	
   	  @ogxtrainee = Ogxtrainee.search_estado(params[:search],params[:page],params[:estado_id])   
   else
   	 if params[:cv] then   	 
   	 		@ogxtrainee = Ogxtrainee.search_cv(params[:search],params[:page],params[:cv])
   	 else
   	 		@ogxtrainee = Ogxtrainee.search(params[:search],params[:page])   	 		   	 
   	 end
	 end
	
    respond_to do |format|
    	format.js { render :partial => 'ogxtrainee_table' }
      format.html 
      format.xml  { render :xml => @ogxtrainee }
    end
  end


# Recupera el ogxtrainee que va a ser mostrado en pantalla.
  def show
    @ogxtrainee = Ogxtrainee.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @ogxtrainee }
    end
  end

# Prepara el ogxtrainee que va a ser posteriormente creado.
  def new
    @ogxtrainee = Ogxtrainee.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @ogxtrainee }
    end
  end

# Recupera el ogxtrainee que va a ser editado.
  def edit
    @ogxtrainee = Ogxtrainee.find(params[:id])
  end

# Crea el ogxtrainee.
  def create
    @ogxtrainee = Ogxtrainee.new(params[:ogxtrainee])
    respond_to do |format|
      if @ogxtrainee.save
        format.js
        format.html { redirect_to(ogxtrainees_path) }
        format.xml  { render :xml => @ogxtrainee, :status => :created, :location => @ogxtrainee }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @ogxtrainee.errors, :status => :unprocessable_entity }
      end
    end
  end

# Actualiza el ogxtrainee.
  def update
    @ogxtrainee = Ogxtrainee.find(params[:id])

    respond_to do |format|
      if @ogxtrainee.update_attributes(params[:ogxtrainee])
        flash[:notice] = 'ogxtrainee was successfully updated.'
        format.html { redirect_to(ogxtrainees_path) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @ogxtrainee.errors, :status => :unprocessable_entity }
      end
    end
  end

# Elimina el ogxtrainee.
  def destroy
  
    @ogxtrainee = Ogxtrainee.find(params[:id])
    @ogxtrainee.destroy

    respond_to do |format|
      format.html { redirect_to(ogxtrainees_path) }
      format.xml  { head :ok }
    end
  end
  
 # Realiza busquedas por tags del cv ingresado. 
	def buscar
		target = params[:search].split(' ')
		@ogxcurriculums = Ogxcurriculum.find(:all)
		trainees_id = []
		for curr in @ogxcurriculums
			if curr.has_tags(target) then
				trainees_id.push(curr.ogxtrainee_id)
			end
		end

		@ogxtrainee = Ogxtrainee.search_individuals(params[:search],params[:page],trainees_id)
		
		respond_to do |format|
			format.js
			format.html { render :action => 'index' }
		end
	end
end
